package com.darly.book.presenter;


import com.darly.book.model.bean.ChapterInfoBean;
import com.darly.book.model.remote.BookApi;
import com.darly.book.presenter.contract.ReadContract;
import com.darly.book.ui.base.RxPresenter;
import com.darly.book.utils.Constant;
import com.darly.book.widget.page.TxtChapter;
import com.darly.dlcommon.common.dlog.DLog;
import com.darly.dlcommon.retrofit.RxjavaRetrofitRequestUtil;

import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;

import io.reactivex.Single;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class ReadPresenter extends RxPresenter<ReadContract.View>
        implements ReadContract.Presenter {
    private static final String TAG = "ReadPresenter";

    private Subscription mChapterSub;

    @Override
    public void loadCategory(String bookId) {

//        Disposable disposable = RxjavaRetrofitRequestUtil.getInstance().getWithHost(BookApi.class, Constant.API_BASE_URL).getBookChapterPackage(bookId, "chapter")
//                .map(bean -> {
//                    if (bean.getMixToc() == null) {
//                        return new ArrayList<BookChapterBean>(1);
//                    } else {
//                        return bean.getMixToc().getChapters();
//                    }
//                }).doOnSuccess(new Consumer<List<BookChapterBean>>() {
//                    @Override
//                    public void accept(List<BookChapterBean> bookChapterBeen) throws Exception {
//                        //进行设定BookChapter所属的书的id。
//                        for (BookChapterBean bookChapter : bookChapterBeen) {
//                            bookChapter.setId(MD5Utils.strToMd5By16(bookChapter.getLink()));
//                            bookChapter.setBookId(bookId);
//                        }
//                    }
//                })
//                .compose(RxUtils::toSimpleSingle)
//                .subscribe(
//                        beans -> {
//                            mView.showCategory(beans);
//                        }
//                        ,
//                        e -> {
//                            DLog.e(e);
//                        }
//                );

//        addDisposable(disposable);
    }

    @Override
    public void loadChapter(String bookId, List<TxtChapter> bookChapters) {
        int size = bookChapters.size();

        //取消上次的任务，防止多次加载
        if (mChapterSub != null) {
            mChapterSub.cancel();
        }

        List<Single<ChapterInfoBean>> chapterInfos = new ArrayList<>(bookChapters.size());
        ArrayDeque<String> titles = new ArrayDeque<>(bookChapters.size());

        // 将要下载章节，转换成网络请求。
        for (int i = 0; i < size; ++i) {
            TxtChapter bookChapter = bookChapters.get(i);
            // 网络中获取数据
            Single<ChapterInfoBean> chapterInfoSingle =
                    RxjavaRetrofitRequestUtil.getInstance().getWithHost(BookApi.class, Constant.API_BASE_URL).getChapterInfoPackage(bookChapter.getLink())
                            .map(bean -> bean.getChapter());

            chapterInfos.add(chapterInfoSingle);

            titles.add(bookChapter.getTitle());
        }

        Single.concat(chapterInfos)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(
                        new Subscriber<ChapterInfoBean>() {
                            String title = titles.poll();

                            @Override
                            public void onSubscribe(Subscription s) {
                                s.request(Integer.MAX_VALUE);
                                mChapterSub = s;
                            }

                            @Override
                            public void onNext(ChapterInfoBean chapterInfoBean) {
                                //存储数据
//                                BookRepository.getInstance().saveChapterInfo(
//                                        bookId, title, chapterInfoBean.getBody()
//                                );
                                mView.finishChapter();
                                //将获取到的数据进行存储
                                title = titles.poll();
                            }

                            @Override
                            public void onError(Throwable t) {
                                //只有第一个加载失败才会调用errorChapter
                                if (bookChapters.get(0).getTitle().equals(title)) {
                                    mView.errorChapter();
                                }
                                DLog.e(t);
                            }

                            @Override
                            public void onComplete() {
                            }
                        }
                );
    }

    @Override
    public void detachView() {
        super.detachView();
        if (mChapterSub != null) {
            mChapterSub.cancel();
        }
    }

}
